package com.jiantong.web.open.app.work;

import com.jiantong.work.model.BeastsBreedStockMessage;
import com.jiantong.work.service.BeastsBreedStockMessageReadService;
import com.jiantong.work.service.BeastsBreedStockMessageWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

/**
 * 数据分析 - 存栏合计统计 zyp
 * Created by mz03 on 2017/4/17.
 */
@Slf4j
@OpenBean
public class BeastsBreedStockMessageApis {
    @RpcConsumer
    private BeastsBreedStockMessageReadService beastsBreedStockMessageReadService;

    @RpcConsumer
    private BeastsBreedStockMessageWriteService beastsBreedStockMessageWriteService;

    /*
    根据ID查询
     */
    @OpenMethod(
            key = "find.beastsBreedStockMessage.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public BeastsBreedStockMessage findById(Long id){
        Response<BeastsBreedStockMessage> response = beastsBreedStockMessageReadService.findById(id);
        if (!response.isSuccess()){
            log.error("failed to find BeastsBreedStockMessage reocrd by id = {}, cause : {}",id,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

     /*
        根据type和指定天数 查询
        返回一个map  key:第几天  value:存栏总和
        其中从上至下是记录最近往后的每一天中的各个存栏合计总和
     */
    @OpenMethod(
            key = "find.stockQuantity.by.type.and.days",
            paramNames = {"type","limit"},
            httpMethods = RequestMethod.GET
    )
    public Map<Integer,Object> findByType(Integer type,Integer limit){
        Map<String,Object> map = new HashedMap();
        if (limit==null){
            limit = 1;
        }
        if (type != null) {
            map.put("type", type);
            map.put("limit", limit);
        }else {
            log.error("type not null!");
        }
        Response<Map<Integer,Object>> mapResponse  = beastsBreedStockMessageReadService.findByType(map);
        if (!mapResponse.isSuccess()){
            log.error("failed to find BeastsBreedStockMessage reocrd by type = {}, cause : {}",type,mapResponse.getError());
            throw new OPServerException(mapResponse.getError());
        }
        return mapResponse.getResult();
    }

    /**
     * 只查找最新7天中的每天的 所有种类(type)的最新存栏合计总和
     */
    @OpenMethod(
            key = "find.stockQuantity.7day.all.type",
            httpMethods = RequestMethod.GET
    )
    public Map<String,Object> findByType(){


        return null;
    }

}
